<?php
// +----------------------------------------------------------------------
// | 云南喵云科技
// +----------------------------------------------------------------------
// | 官方网站: https://www.meoyun.com
// +----------------------------------------------------------------------
// | Author: 喵大大
// +----------------------------------------------------------------------
// | Email: mmteen@sina.com
// +----------------------------------------------------------------------
// | Date: 2021/10/28/028
// +----------------------------------------------------------------------
// | Time: 17:55
// +----------------------------------------------------------------------
namespace App\RenderDriver;

use EasySwoole\Template\RenderInterface;
class Template implements RenderInterface
{
    protected $template;

    function __construct()
    {
        $config = [
            'view_path'=>EASYSWOOLE_ROOT.'/App/Views/',
            'cache_path'=>EASYSWOOLE_ROOT.'/Temp/runtime/',
        ];

        $this->template = new \think\Template($config);
    }

    public function render(string $template, ?array $data = null, ?array $options = null): ?string
    {
        // TODO: Implement render() method.

        ob_start();

        $this->template->assign($data);

        $this->template->fetch($template);

        $content = ob_get_contents() ;

        return $content;
    }

    public function onException(\Throwable $throwable, $arg): string
    {
        $msg = "{$throwable->getMessage()} at file:{$throwable->getFile()} line:{$throwable->getLine()}";
        trigger_error($msg);
        return $msg;
    }
}